package com.henu.zmb;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;

import com.henu.R;
import com.henu.zmb.DBhelper.DBhelper;
import com.henu.zmb.adapter.UserGoods_adapter;
import com.henu.zmb.entity.Goods;
import com.henu.zmb.entity.User_Goods;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class User_Collect_Activity extends AppCompatActivity {

    ListView lv_goods;
    UserGoods_adapter ug_adapter;
    ArrayList<User_Goods> user_goods = new ArrayList<User_Goods>();
    ImageButton auc_return;

    protected void show_collectgood(){
        super.onResume();
        user_goods.clear();
        BmobQuery<User_Goods> query = new BmobQuery<User_Goods>();
        // 通过类别id查找当前分类下的所有物品
        query.addWhereEqualTo("User_Id", new BmobPointer(BmobUser.getCurrentUser()));
        query.include("Goods_Id");
        query.setLimit(50);
        //执行查询方法
        query.findObjects(new FindListener<User_Goods>() {
            @Override
            public void done(List<User_Goods> object, BmobException e) {
                if(e==null){
                    user_goods =(ArrayList)object;
                    ug_adapter.addAllData(user_goods); // 绑定数据
                    ug_adapter.notifyDataSetChanged();// 显示
                }else{
                    System.out.println("bmob失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user__collect);
        auc_return = (ImageButton)findViewById(R.id.auc_return);
        lv_goods = (ListView)findViewById(R.id.auc_lvgoods);
        ug_adapter = new UserGoods_adapter(this);
        lv_goods.setAdapter(ug_adapter);
        show_collectgood();// 显示信息
        // 物品详情 点击事件
        lv_goods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(User_Collect_Activity.this,GoodsDetails_Activity.class); // 设置跳转的路径
                Goods good = user_goods.get(position).getGoods_Id(); // 取出内容
                intent.putExtra("good", good); // 需要实例化Goods
                startActivity(intent); // 跳转
            }
        });
        // 删除收藏
        lv_goods.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(User_Collect_Activity.this);
                dialog.setTitle("是否删除");
                dialog.setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DBhelper<User_Goods> db = new DBhelper<User_Goods>();
                        db.Delete(user_goods.get(position));
                        show_collectgood();// 显示信息
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.create();
                dialog.show();

                return false;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        show_collectgood();
    }
}
